<template>
  <div class="employees-container">
    <div class="app-container">
      <el-card>
        <!-- 表单 -->
        <el-form ref="userInfoRef" label-width="120px" :model="userInfoForm" :rules="userInfoRules" style="margin-left: 120px; margin-top:30px">
          <el-form-item label="姓名:" prop="username">
            <el-input v-model="userInfoForm.username" style="width:300px" />
          </el-form-item>

          <el-form-item label="手机">
            <el-input v-model="userInfoForm.mobile" disabled style="width:300px" />
          </el-form-item>

          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfoForm.timeOfEntry"
              type="date"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>

          <el-form-item label="员工头像">
            <!-- <upload-img 
              :value="userInfoForm.staffPhoto"
              @input="userInfoForm.staffPhoto = $event"
            /> -->
            <upload-img 
              v-model="userInfoForm.staffPhoto"
            />
          </el-form-item>

          <el-form-item label="员工头像">
            <!-- <input type="text" v-model="name"> -->
            <input type="text" :value="name" @input="nameInputChange">
          </el-form-item>

          <!-- 保存个人信息 -->
          <el-row class="inline-info" type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" @click="submit">保存更新</el-button>
              <el-button @click="$router.back()">返回</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { updateUserDetail } from '@/api/employee'
import UploadImg from '@/components/UploadImg'
export default {
  name: 'EmployeeDetail',
  components: {
    UploadImg
  },
  data() {
    return {
      userInfoForm: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        staffPhoto: ''
      },
      userInfoRules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ]
      },
      name: 'zhangsan'
    }
  },
  created() {
    this.fetchUserDetailById()
  },
  methods: {
    // 获取员工信息
    async fetchUserDetailById() {
      const res = await getUserDetailById(this.$route.query.id)
      // console.log(res)
      this.userInfoForm = res.data
    },
    // 保存更新
    submit() {
      // 兜底校验
      this.$refs.userInfoRef.validate(async valid => {
        if (!valid) return
        // 调用更新用户信息接口
        const res = await updateUserDetail(this.userInfoForm)
        // 提示用户
        this.$message.success(res.message)
        // 刷新用户信息
        if (this.userInfoForm.id === this.$store.state.user.userInfo.id) {
          this.$store.dispatch('user/fetchUserInfo')
        }
      })
    },
    // 获得最新的图片
    // getUrl(url) {
    //   this.userInfoForm.staffPhoto = url
    // },
    nameInputChange(e) {
      this.name = e.target.value
    }
  }
}
</script>
